<?php
class M_profiles extends MY_Model {

	protected $_table = 'profiles';
	protected $_uid = 'profile_id';
	protected $_timestamp = false;
	
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Get Student Full Profile Per Enrollment
	 * @param int $profile_id
	 * @return array of objects
	 */
	public function get_student_full_profile( $profile_id )
	{
		$this->load->model('M_enrollees');
		$this->load->model('M_teachers');
		$this->load->model('M_sections');
		$this->load->model('M_billing_form');
    $this->load->model('M_student_payment_record');

		$ibalik = false;

		//Get All Enrollment Record of Student
		$enrollments = $this->M_enrollees->get_enrollments_prof($profile_id);
		foreach ($enrollments as $k => $e) {
			$ibalik[$k]['profile'] = $e;
			$ibalik[$k]['enrollment'] = $this->M_enrollees->get_enrollee_prof($e->e_id); 
			$ibalik[$k]['section'] = $this->M_sections->get_section_data($e->e_id);
			$ibalik[$k]['grade'] = $this->M_teachers->get_student_subjects(TRUE, $e->e_id);
			$ibalik[$k]['remark'] = $this->M_teachers->_get_student_remarks($e->e_id);
			$ibalik[$k]['attendance'] = $this->M_teachers->get_student_attendance_detailed($e->e_id);
			$ibalik[$k]['schedule'] = $this->M_enrollees->get_all_schedule_by_student_subjects($e->e_id);
			$ibalik[$k]['billing'] =  $this->M_billing_form->get_student_billings($e->e_id);
      $ibalik[$k]['prs'] =  $this->M_student_payment_record->get_payment_record_by_enrollment_id($e->e_id);
			$ibalik[$k]['soa'] =  $this->soa($e->e_id);
		}

		// vp($ibalik);

		return $ibalik;
	}


	public function soa($e_id)
	{
    $this->load->model(array('M_enrollees','M_scholarships','M_fees'));
    $latest_sy = $this->M_enrollees->get_sy_id($e_id); 
    
		unset($v_data);
    
		if($latest_sy->e_ay_id == 3){
			$v_data['official_student'] = TRUE;
      
			$prev_e_id = $this->M_enrollees->get_previous_e_id($e_id); 
			
			if($prev_e_id)
			{
				$v_data['payment_mode'] = $this->M_enrollees->get_student_payment_mode($prev_e_id->e_id,2);
				$v_data['student_deductions'] = $this->M_scholarships->get_student_deductions($prev_e_id->e_id,2);
				$v_data['student_scholarships'] = $this->M_scholarships->get_student_scholarships_deducted($prev_e_id->e_id,2);
				$v_data['total_stud_deductions'] = $this->M_scholarships->sum_all_student_deductions($prev_e_id->e_id,2);
				
				$v_data['tuit_fees'] = $this->M_enrollees->get_student_fees($prev_e_id->e_id,2,'tuit');
				$v_data['misc_fees'] = $this->M_enrollees->get_student_fees($prev_e_id->e_id,2,'misc');
				$v_data['cca_fees'] = $this->M_enrollees->get_student_fees($prev_e_id->e_id,2,'cca');
				$v_data['elec_fees'] = $this->M_enrollees->get_student_fees($prev_e_id->e_id,2,'elec');
				
				$v_data['enrollee_data'] = $enrolle_data = $this->M_enrollees->get_enrollee_prof($prev_e_id->e_id);
				$v_data['sum_paid_fees'] = $sum_paid_fees = $this->M_enrollees->get_sum_of_paid_fees($prev_e_id->e_id,2);
				$v_data['sum_amount_due'] = $sum_amount_due =$this->M_enrollees->get_sum_of_total_payables($prev_e_id->e_id,2);
				$v_data['total_payables'] = $this->M_enrollees->get_sum_of_total_payables($prev_e_id->e_id,2);
				$v_data['electives_total'] = $electives_total =$this->M_enrollees->get_sum_of_student_electives($prev_e_id->e_id,2);
				$v_data['profile_id'] = NULL;
				$v_data['enrollment_id'] = $prev_e_id->e_id;
				$v_data['level_id'] = $v_data['enrollee_data']->level_id;
				$v_data['foreign_fees_applied'] = $this->M_enrollees->check_student_nationality($prev_e_id->e_id,$enrolle_data);
				$v_data['eid_enc'] = $this->hs->encrypt(intval($prev_e_id->e_id),intval(time()));
				
				
				//check if student is fully paid
				$v_data['finance_status'] = $this->_check_student_payment_status($prev_e_id->e_id,2);
			}
		}else{
			$v_data['official_student'] = TRUE;
			$this->load->model(array('M_enrollees','M_scholarships','M_fees'));
      $sy = $latest_sy->e_ay_id;
      
			//get student's payment mode
			$v_data['payment_mode'] = $this->M_enrollees->get_student_payment_mode($e_id,$sy);
			$v_data['student_deductions'] = $this->M_scholarships->get_student_deductions($e_id,$sy);
			$v_data['student_scholarships'] = $this->M_scholarships->get_student_scholarships_deducted($e_id,$sy);
			$v_data['total_stud_deductions'] = $this->M_scholarships->sum_all_student_deductions($e_id,$sy);
			
			$v_data['tuit_fees'] = $this->M_enrollees->get_student_fees($e_id,$sy,'tuit');
			$v_data['misc_fees'] = $this->M_enrollees->get_student_fees($e_id,$sy,'misc');
			$v_data['cca_fees'] = $this->M_enrollees->get_student_fees($e_id,$sy,'cca');
			$v_data['elec_fees'] = $this->M_enrollees->get_student_fees($e_id,$sy,'elec');
			
			$v_data['enrollee_data'] = $enrolle_data = $this->M_enrollees->get_enrollee_prof($e_id);
			$v_data['sum_paid_fees'] = $sum_paid_fees = $this->M_enrollees->get_sum_of_paid_fees($e_id,$sy);
			$v_data['sum_amount_due'] = $sum_amount_due =$this->M_enrollees->get_sum_of_total_payables($e_id,$sy);
			$v_data['total_payables'] = $this->M_enrollees->get_sum_of_total_payables($e_id,$sy);
			$v_data['electives_total'] = $electives_total =$this->M_enrollees->get_sum_of_student_electives($e_id,$sy);
			$v_data['profile_id'] = NULL;
			$v_data['enrollment_id'] = $e_id;
			$v_data['level_id'] = $v_data['enrollee_data']->level_id;
			$v_data['foreign_fees_applied'] = $this->M_enrollees->check_student_nationality($e_id,$enrolle_data);
			$v_data['eid_enc'] = $this->hs->encrypt(intval($e_id),intval(time()));

			//check if student is fully paid
			$v_data['finance_status'] = $this->_check_student_payment_status($e_id);
		}
		return $v_data;
	}

	//check if student is fully paid or not
	private function _check_student_payment_status($enrollment_id = '',$sy=FALSE)
	{
		$this->load->model('M_enrollees');
		$this->load->model('M_fees');

		$fps = $this->M_fees->_specific('enrollments','e_fullypaid',['e_id'=>$enrollment_id],TRUE);

		//other payments not in sum_amount_due
		if($sy){
			$electives_total =$this->M_enrollees->get_sum_of_student_electives($enrollment_id,$sy);
		}else{
			$electives_total =$this->M_enrollees->get_sum_of_student_electives($enrollment_id,$this->current_school_year);
		}
		
		//total paid
		if($sy){
			$sum_paid_fees = $this->M_enrollees->get_sum_of_paid_fees($enrollment_id,$sy);
		}else{
			$sum_paid_fees = $this->M_enrollees->get_sum_of_paid_fees($enrollment_id,$this->current_school_year);
		}
		
		//total of payable of student
		if($sy){
			$sum_amount_due =$this->M_enrollees->get_sum_of_total_payables($enrollment_id,$sy);
		}else{
			$sum_amount_due =$this->M_enrollees->get_sum_of_total_payables($enrollment_id,$this->current_school_year);
		}
		
		//students current payment plan
		if($sy){
			$pm =$this->M_enrollees->get_student_payment_mode($enrollment_id,$sy);
		}else{
			$pm =$this->M_enrollees->get_student_payment_mode($enrollment_id);
		}

		$test2 = $this->M_fees->get_student_current_payment_mode_and_due_by_enrollment_id($enrollment_id);

		if($pm !== FALSE )
		{
			if(($sum_amount_due + $electives_total) - $sum_paid_fees <= 0 )
			{
				if($fps->e_fullypaid == 0)
				{
					if($sy){
						$this->M_student_payment_record->update_fully_paid_status($enrollment_id,FALSE,$sy);
					}else{
						$this->M_student_payment_record->update_fully_paid_status($enrollment_id);
					}
				}

			return 'Fully Paid';
			}else{
				if($fps->e_fullypaid == 1)
				{
					if($sy){
						$this->M_student_payment_record->update_fully_paid_status($enrollment_id,TRUE,$sy);
					}else{
						$this->M_student_payment_record->update_fully_paid_status($enrollment_id,TRUE);
					}
				}
			return 'NOT Fully Paid';
			}
		}else{
		return 'Student has no payment plan';
		}
	}
}	
